//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.滑动窗口.定长滑动窗口;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class demo_438 {
    public demo_438() {
    }

    public static void main(String[] args) {
        System.out.println(findAnagrams("cbaebabacd", "abc"));
    }

    public static List<Integer> findAnagrams(String s, String p) {
        List<Integer> res = new ArrayList();
        int n = s.length();
        int k = p.length();
        int left = 0;
        HashMap<Character, Integer> map = new HashMap();

        for(char c : p.toCharArray()) {
            map.put(c, (Integer)map.getOrDefault(c, 0) + 1);
        }

        for(int i = 0; i < n; ++i) {
            char c = s.charAt(i);
            if (p.contains(String.valueOf(c))) {
                map.put(c, (Integer)map.getOrDefault(c, 0) - 1);
                if ((Integer)map.get(c) == 0) {
                    map.remove(c);
                }
            }

            if (i >= k - 1) {
                if (map.isEmpty()) {
                    res.add(left);
                }

                char out = s.charAt(left);
                if (p.contains(String.valueOf(out))) {
                    map.put(out, (Integer)map.getOrDefault(out, 0) + 1);
                    if ((Integer)map.get(out) == 0) {
                        map.remove(out);
                    }
                }

                ++left;
            }
        }

        return res;
    }
}
